package com.example;


import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.config.*;
import com.baomidou.mybatisplus.generator.config.rules.DateType;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
import com.baomidou.mybatisplus.generator.engine.VelocityTemplateEngine;
import org.apache.velocity.VelocityContext;
import org.checkerframework.checker.units.qual.C;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

/**
 * @author Xiongx
 * @version 1.0
 * @date 2021/6/11 17:23
 * @since JDK 1.8
 */
public class CodeGenerator {

    private static String username="";

    private static String password="";

    private static String moudleName="business";

    private static String basePath="/src/main/java";

    private static String resourceMapperPath = "mapper";

    public static void main(String[] args) {
        CodeGenerator codeGenerator=new CodeGenerator();
        String packagePath = codeGenerator.getClass().getPackage().getName();

        codeGenerator.genCode("sys_video_play_record",packagePath);

    }


    public void genCode(String tableName,String packagePath){
        //构建代码自动生成器对象
        AutoGenerator mpg=new AutoGenerator();
        // 全局配置
        GlobalConfig gc = new GlobalConfig();
        String projectPath = System.getProperty("user.dir");//获取当前系统目录
        gc.setOutputDir(projectPath + basePath);
        gc.setAuthor("xiong");
        gc.setOpen(false);//是否打开资源管理器（是否打开Windows文件夹）
        gc.setFileOverride(false);//是否覆盖原来生成的
        gc.setServiceName("%sService");//去掉service前缀的I
        gc.setIdType(IdType.AUTO);//id自动生成策略
        gc.setDateType(DateType.ONLY_DATE);//日期生成策略
        gc.setSwagger2(true);// 实体属性 Swagger2 注解
        mpg.setGlobalConfig(gc);

        // 数据源配置
        DataSourceConfig dsc = new DataSourceConfig();
        dsc.setUrl("jdbc:mysql://127.0.0.1:3306/spring-video?useSSL=false&useUnicode=true&characterEncoding=UTF-8&serverTimezone=GMT%2B8&zeroDateTimeBehavior=convertToNull");
        // dsc.setSchemaName("public");
        dsc.setDriverName("com.mysql.jdbc.Driver");
        dsc.setUsername("root");
        dsc.setPassword("123456");
        mpg.setDataSource(dsc);

        // 包配置
        PackageConfig pc = new PackageConfig();
        // pc.setModuleName("SpringBootNoteManager");
        pc.setParent(packagePath+"."+moudleName);
        pc.setEntity("entity");
        pc.setService("service");
        pc.setMapper("mapper");
        pc.setController("controller");
        mpg.setPackageInfo(pc);
        // 策略配置
        StrategyConfig strategy = new StrategyConfig();
        strategy.setNaming(NamingStrategy.underline_to_camel);//设置包命名的规则 下划线转成驼峰命名
        strategy.setColumnNaming(NamingStrategy.underline_to_camel);//列
        strategy.setControllerMappingHyphenStyle(true); // 将Controller的URL映射采用驼峰转连字符的方式
        strategy.setInclude(tableName);//设置要映射的表
        strategy.setEntityLombokModel(true);
        strategy.setLogicDeleteFieldName("deletestatus");//逻辑删除

        TemplateConfig templateConfig = new TemplateConfig()
                .setServiceImpl("template/code/serviceImpl.java")
                .setMapper("template/code/mapper.java")
                .setEntity("template/code/entity.java")
                .setService("template/code/service.java")
                .setController("template/code/controller.java"); // 设置Controller代码的模板文件路径


        VelocityTemplateEngine templateEngine = new VelocityTemplateEngine();




        mpg.setTemplate(templateConfig);
        mpg.setTemplateEngine(templateEngine);
        // TableField gmt=new TableField("createtime", FieldFill.INSERT);自动填充
        // ArrayList<TableField> list=new ArrayList<>();
        // list.add(gmt);
        //strategy.setTableFillList(list);
        mpg.setStrategy(strategy);

        //执行
        mpg.execute();
    }
}
